﻿Public Class frmThanhTich

    Sub LoadDanhSach()
        Dim _tt As DataTable
        _tt = ThanhTichDAO.SelectAll()
        dgvTTThanhTich.DataSource = _tt
    End Sub

    Sub Display()
        If (dgvTTThanhTich.SelectedRows.Count > 0) Then
            txtMATHANHTICH.Text = dgvTTThanhTich.SelectedRows(0).Cells(0).Value.ToString()
            txtTENTHANHTICH.Text = dgvTTThanhTich.SelectedRows(0).Cells(1).Value.ToString()
        End If
    End Sub

    Private Sub frmThanhTich_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvTTThanhTich.Rows.Clear()
        LoadDanhSach()
    End Sub

    Private Sub dgvTTThanhTich_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTTThanhTich.CellContentClick
        Display()
    End Sub

    Private Sub dgvTTThanhTich_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvTTThanhTich.SelectionChanged
        Display()
    End Sub

    Private Sub frmThanhTich_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvTTThanhTich.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _tt As ThanhTichDTO = New ThanhTichDTO()
                _tt.MaThanhTich = txtMATHANHTICH.Text
                _tt.TenThanhTich = txtTENTHANHTICH.Text
                Try
                    ThanhTichDAO.Update(_tt)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
                LoadDanhSach()
            End If
        End If
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click
        Dim _tt As ThanhTichDTO = New ThanhTichDTO()
        _tt.TenThanhTich = txtTENTHANHTICH.Text
        Try
            ThanhTichDAO.Insert(_tt)
            MessageBox.Show("Thêm thành tích thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvTTThanhTich.SelectedRows.Count > 0) Then
            Dim _tt As ThanhTichDTO = New ThanhTichDTO()
            _tt.MaThanhTich = txtMATHANHTICH.Text
            _tt.TenThanhTich = txtTENTHANHTICH.Text
            Try
                ThanhTichDAO.Update(_tt)
                MessageBox.Show("Cập nhật thành tích thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _tt As ThanhTichDTO = New ThanhTichDTO()
        _tt.MaThanhTich = txtMATHANHTICH.Text
        Try
            ThanhTichDAO.Delete(_tt)
            MessageBox.Show("Xóa thành tích thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub
End Class